package org.pp.config;


import cn.hutool.core.util.StrUtil;
import org.pp.core.Constants;
import org.redisson.Redisson;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RedissonConfig {

    @Value("${spring.data.redis.host}")
    private String host;
    @Value("${spring.data.redis.port}")
    private Integer port;
    @Value("${spring.data.redis.password}")
    private String password;
    @Value("${spring.data.redis.database}")
    private Integer database;


    @Bean
    public RedissonClient getClient(){
        Config config= new Config();
        config.useSingleServer().setAddress(
                StrUtil.format("redis://{}:{}",host,port)
        ).setPassword(password)
                .setDatabase(database);
        return Redisson.create(config);
    }

    @Bean
    @Primary
    public RQueue<String> orderQueue(RedissonClient redissonClient) {
        return redissonClient.getQueue(Constants.ORDER_QUEUE_NAME);
    }

    @Bean(destroyMethod = "destroy")
    public RDelayedQueue<String> delayedOrderQueue(RedissonClient redissonClient, RQueue<String> orderQueue) 	{
        return  redissonClient.getDelayedQueue(orderQueue);
    }

}
